package com.zhdl.modules.prefiresys.service.impl;


import com.project.common.service.impl.BaseServiceImpl;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.zhdl.modules.prefiresys.entity.DetectorSwitchRec;
import com.zhdl.modules.prefiresys.entity.QDetectorSwitchRec;
import com.zhdl.modules.prefiresys.repository.DetectorSwitchRecRepository;
import com.zhdl.modules.prefiresys.service.DetectorSwitchRecService;
import com.zhdl.modules.prefiresys.web.request.SwitchRecListRequest;
import com.zhdl.modules.prefiresys.web.response.SwitchRecListResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class DetectorSwitchRecServiceImpl extends BaseServiceImpl<String, DetectorSwitchRec, DetectorSwitchRecRepository> implements DetectorSwitchRecService {
    /**
     * 查询探测器开关量明细数据
     */
    @Override
    public List<SwitchRecListResponse> findAllRecord(SwitchRecListRequest switchRecListRequest) {
        QDetectorSwitchRec qDetectorSwitchRec = QDetectorSwitchRec.detectorSwitchRec;
        JPAQuery<SwitchRecListResponse> query = jpaQueryFactory()
                .select(Projections.constructor(SwitchRecListResponse.class,
                        qDetectorSwitchRec.id,
                        qDetectorSwitchRec.createDate,
                        qDetectorSwitchRec.detectorId,
                        qDetectorSwitchRec.di1,
                        qDetectorSwitchRec.di2,
                        qDetectorSwitchRec.di3,
                        qDetectorSwitchRec.di4,
                        qDetectorSwitchRec.do1,
                        qDetectorSwitchRec.do2,
                        qDetectorSwitchRec.do3,
                        qDetectorSwitchRec.do4))
                .from(qDetectorSwitchRec);
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qDetectorSwitchRec.createDate.between(switchRecListRequest.getStartTime(), switchRecListRequest.getEndTime()));
        builder.and(qDetectorSwitchRec.detectorId.eq(switchRecListRequest.getDetectorId()));

        List<SwitchRecListResponse> fetch = query.where(builder)
                .orderBy(qDetectorSwitchRec.createDate.asc())
                .fetch();
        return fetch;
    }
}
